
<!--
 * @Description: 工程列表
 * @Author: charles
 * @Date: 2021-12-14 22:06:30
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-12-22 15:10:43
-->
<template>
  <div>
    <!-- 按钮区域 -->
    <div>
      <!-- 新增按钮 -->
      <el-button @click="openHandler" type="primary" size="small"
        >添加</el-button
      >
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table :data="tableData.list" style="width: 100%" size="small">
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item prop="serial_number" label="工程编号">
                <span>{{ props.row.serial_number }}</span>
              </el-form-item>
              <el-form-item label="工程名称">
                <span>{{ props.row.name }}</span>
              </el-form-item>

              <el-form-item label="工程类型">
                <span>{{ props.row.type }}</span>
              </el-form-item>
              <el-form-item label="工程地址">
                <span>{{ props.row.address }}</span>
              </el-form-item>
              <el-form-item label="工程状态">
                <span>{{ props.row.status }}</span>
              </el-form-item>
              <el-form-item label="区域经理">
                <span>{{ props.row.charge_name }}</span>
              </el-form-item>
              <el-form-item label="客户姓名">
                <span>{{ props.row.customer.name }}</span>
              </el-form-item>
              <el-form-item prop="customer.telephone" label="客户手机号码">
                <span>{{ props.row.customer.telephone }}</span>
              </el-form-item>
              <el-form-item label="所在位置经度">
                <span>{{ props.row.longitude }}</span>
              </el-form-item>
              <el-form-item label="所在位置纬度">
                <span>{{ props.row.latitude }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column
          prop="serial_number"
          label="工程编号"
          width="100"
        ></el-table-column>
        <el-table-column prop="name" label="工程名称"> </el-table-column>
        <el-table-column
          prop="type"
          label="工程类型"
          width="120"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="charge_name"
          label="区域经理"
          width="120px"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="customer.realname"
          label="客户姓名"
          width="120px"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="customer.telephone"
          label="客户电话"
          width="120px"
          align="center"
        >
        </el-table-column>

        <el-table-column
          prop="status"
          label="工程状态"
          width="80"
          align="center"
        >
          <template v-slot="scope">
            <el-tag
              v-if="scope.row.status == '未绑定'"
              type="info"
              size="mini"
              >{{ scope.row.status }}</el-tag
            >
            <el-tag v-else-if="scope.row.status == '监测中'" size="mini">{{
              scope.row.status
            }}</el-tag>
            <el-tag v-else type="danger" size="mini">{{
              scope.row.status
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template v-slot="scope">
            <el-button type="text" @click="toEditHandler(scope.row)"
              >编辑</el-button
            >
            <el-button type="text" @click="toBindHandler(scope.row)"
              >绑定</el-button
            >
            <el-button
              v-if="scope.row.status == '已结束'"
              disabled
              type="text"
              @click="toEndHandler(scope.row.id)"
              >结束</el-button
            >
            <el-button v-else type="text" @click="toEndHandler(scope.row.id)"
              >结束</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--模态框-->
    <el-dialog
      :title="title"
      :visible.sync="visible"
      width="50%"
      @close="dialogCloseHandler"
    >
      <el-form :model="form" :inline="true" ref="engineerForm" :rules="rules">
        <el-form-item
          prop="serial_number"
          label="工程编号"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="form.serial_number"
            placeholder="请输入工程编号"
          ></el-input>
        </el-form-item>
        <el-form-item
          prop="name"
          label="工程名称"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.name" placeholder="请输入工程名称"></el-input>
        </el-form-item>
        <el-form-item
          prop="type"
          label="工程类型"
          :label-width="formLabelWidth"
        >
          <el-select v-model="form.type" placeholder="请选择工程类型">
            <el-option label="园区" value="园区"></el-option>
            <el-option label="住宅" value="住宅"></el-option>
            <el-option label="医院" value="医院"></el-option>
            <el-option label="写字楼" value="写字楼"></el-option>
            <el-option label="景区" value="景区"></el-option>
            <el-option label="学校" value="学校"></el-option>
            <el-option label="商场" value="商场"></el-option>
            <el-option label="建筑工地" value="建筑工地"></el-option>
            <el-option label="事业单位" value="事业单位"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          prop="longitude"
          label="位置经度"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="form.longitude"
            placeholder="请输入位置经度"
          ></el-input>
        </el-form-item>
        <el-form-item
          prop="latitude"
          label="位置纬度"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="form.latitude"
            placeholder="请输入位置纬度"
          ></el-input>
        </el-form-item>
        <el-form-item
          prop="address"
          label="工程地址"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="form.address"
            auto-complete="off"
            placeholder="请输入工程地址"
          ></el-input>
        </el-form-item>
        <el-form-item
          prop="customer_name"
          label="客户姓名"
          :label-width="formLabelWidth"
        >
          <el-input
            v-if="form.id"
            v-model="form.customer_name"
            disabled
            placeholder="请输入客户姓名"
          ></el-input>
          <el-input
            v-else
            v-model="form.customer_name"
            placeholder="请输入客户姓名"
          ></el-input>
        </el-form-item>
        <el-form-item
          prop="customer_telephone"
          label="客户手机号"
          :label-width="formLabelWidth"
        >
          <el-input
            v-if="form.id"
            v-model="form.customer_telephone"
            disabled
            placeholder="请输入客户手机号"
          ></el-input>
          <el-input
            v-else
            v-model="form.customer_telephone"
            placeholder="请输入客户手机号"
          ></el-input>
        </el-form-item>
        <el-form-item
          prop="charge_id"
          label="区域经理"
          :label-width="formLabelWidth"
        >
          <el-select v-model="form.charge_id" placeholder="请选择区域经理">
            <el-option
              v-for="(item, index) in charges"
              :key="index"
              :label="item.realname"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="commitHandler">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 分页组件 -->
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="parseInt(tableData.page)"
      :page-size="parseInt(tableData.pageSize)"
      layout="total, sizes, prev, pager, next"
      :total="tableData.total"
      :page-sizes="[5, 10, 15, 20]"
    >
    </el-pagination>
  </div>
</template>
<script>
import { get, postJSON } from "@/utils/request";
export default {
  data() {
    return {
      params: {
        page: 1,
        pageSize: 10,
      },
      //   validate:{},
      tableData: {},
      form: {},
      visible: false,
      title: "",
      formLabelWidth: "100px",

      rules: {
        serial_number: [{ required: true, message: "请输入工程编号" }],
        name: [{ required: true, message: "请输入工程名称" }],
        type: [{ required: true, message: "请输入工程类型" }],
        customer_name: [{ required: true, message: "请输入客户姓名" }],
        customer_telephone: [{ required: true, message: "请输入客户手机号" }],
        longitude: [{ required: true, message: "请输入经度" }],
        latitude: [{ required: true, message: "请输入纬度" }],
        address: [{ required: true, message: "请输入工程地址" }],
        charge_id: [{ required: true, message: "请选择区域经理" }],
      },
      charges: [],
    };
  },
  methods: {
    // 查询所有区域经理

    queryAllCharge() {
      const params = {
        page: 1,
        pageSize: 100,
        role_id: 3,
      };
      // get
      get("/user/pageQuery", params).then((res) => {
        this.charges = res.data.list;
      });
    },
    // 新增按钮点击事件
    openHandler() {
      this.form = {};
      // 先设置模态框的标题
      this.title = "添加工程";
      this.visible = true;
      // 最后打开模态框
    },

    toEndHandler(id) {
      this.$confirm("此操作将取消订单, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        get("/engineer/finish", { id }).then((res) => {
          this.pageQueryEngineers();
          this.$message.success("操作成功");
        });
      });
    },

    // 编辑按钮点击事件
    toEditHandler(row) {
      this.form = {
        id: row.id,
        serial_number: row.serial_number,
        name: row.name,
        type: row.type,
        customer_name: row.customer.realname,
        customer_telephone: row.customer.telephone,
        charge_name: row.charge_name,
        latitude: row.latitude,
        longitude: row.longitude,
        address: row.address,
        charge_id: row.charge_id,
      };
      this.title = "修改工程信息";
      // 打开模态框
      this.visible = true;
    },
    dialogCloseHandler() {
      this.$refs["engineerForm"].resetFields();
    },
    // 绑定按钮点击事件

    toBindHandler(row) {
      // vue中使用编程式导航实现页面跳转
      this.$router.push({
        path: "/am/engineer/Details",
        query: row,
      });
    },
    // page改变处理函数
    handleCurrentChange(page) {
      this.params.page = page;
      this.pageQueryEngineers();
    },
    // pageSize改变处理函数
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize;
      this.pageQueryEngineers();
    },
    // 分页查询工程信息
    pageQueryEngineers() {
      get("/engineer/pageQuery", this.params).then((res) => {
        this.tableData = res.data;
      });
    },
    commitHandler() {
      this.$refs["engineerForm"].validate((valid) => {
        // 表单验证通过后，再去请求后台接口
        if (valid) {
          postJSON("/engineer/saveOrUpdate", this.form).then((res) => {
            // 操作成功之后的业务处理
            // 刷新数据
            this.pageQueryEngineers();
            // 提示成功信息
            this.$message({
              message: "操作成功",
              type: "success",
            });
            // 关闭模态框
            this.visible = false;
          });
        } else {
          // 表单验证失败直接返回false
          return false;
        }
      });
    },
  },
  // vue生命周期的钩子函数
  created() {
    // 页面加载之前，请求工程数据
    this.pageQueryEngineers();
    // 查询所有区域经理
    this.queryAllCharge();
  },
};
</script>